package com.kongwg.preparedStatement.crud;

import com.kongwg.preparedStatement.bean.Customer;
import com.kongwg.util.JDBCUtil;
import org.junit.Test;

import java.io.IOException;
import java.lang.reflect.Field;
import java.sql.*;

/**
 * @author kongwg
 * @create 2021-01-07 17:43
 */
public class CustomerForQuery {

    @Test
    public void testQuery() throws Exception {
        Connection conn = JDBCUtil.getConnetion();
        String sql = "select id,name,email,birth from customers where id = ?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, 1);
        ResultSet resultSet = ps.executeQuery();

        if (resultSet.next()) {
            int id = resultSet.getInt(1);
            String name = resultSet.getString(2);
            String email = resultSet.getString(3);
            Date birth = resultSet.getDate(4);

            Customer customer = new Customer(id, name, email, birth);
            System.out.println(customer);
        }

        JDBCUtil.closeResource(conn, ps, resultSet);
    }


    /**
     * 通用查询customer
     */
    public static Customer queryCustomer(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnetion();

            ps = conn.prepareStatement(sql);

            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            rs = ps.executeQuery();
            //获取列的数量
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            if (rs.next()) {
                Customer customer = new Customer();
                for (int i = 0; i < columnCount; i++) {
                    Object columnValue = rs.getObject(i + 1);

                    String columnName = rsmd.getColumnName(i + 1);

                    Field field = Customer.class.getDeclaredField(columnName);
                    field.setAccessible(true);
                    field.set(customer, columnValue);

                }
                return customer;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeResource(conn, ps, rs);
        }

        return null;
    }

    @Test
    public void testQueryCustomers() {
        String sql = "select id,name,email,birth from customers where id = ?";
        Customer customer = queryCustomer(sql, 4);
        System.out.println(customer);

        String sql1 = "select id,name,email from customers where name = ?";
        Customer customer1 = queryCustomer(sql1, "汪峰");
        System.out.println(customer1);
    }
}
